﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace Bainx.Extend
{
    /// <summary>
    /// XmlHelper 的摘要说明
    /// </summary>
    public class XmlHelper
    {
        string path = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        XmlDocument doc = new XmlDocument();
        /// <summary>
        /// 
        /// </summary>
        public XmlHelper()
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Path"></param>
        public XmlHelper(string Path)
        {
            path = Path;
            doc.Load(path);
        }
        /// <summary>
        /// 
        /// </summary>
        public void Load()
        {
            doc.Load(path);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        public void Load(string filename)
        {
            doc.Load(filename);
            path = filename;
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时返回该属性值，否则返回串联值</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read("/Node", "")
         * XmlHelper.Read("/Node/Element[@Attribute='Name']", "Attribute")
         ************************************************/
        public string Read(string node, string attribute)
        {
            string value = "";
            XmlNode xn = doc.SelectSingleNode(node);
            value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);           
            return value;
        }
        /// <summary>
        /// 
        /// </summary>
        public void Save()
        {
            if (doc != null)
                doc.Save(path);
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="element">元素名，非空时插入新元素，否则在该元素中插入属性</param>
        /// <param name="attribute">属性名，非空时插入该元素属性值，否则插入元素值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * Insert("/Node", "Element", "", "Value")
         * Insert("/Node", "Element", "Attribute", "Value")
         * Insert("/Node", "", "Attribute", "Value")
         ************************************************/
        public void Insert(string node, string element, string attribute, string value)
        {
            XmlNode xn = doc.SelectSingleNode(node);
            if (element.Equals(""))
            {
                if (!attribute.Equals(""))
                {
                    XmlElement xe = (XmlElement)xn;
                    xe.SetAttribute(attribute, value);
                }
            }
            else
            {
                XmlElement xe = doc.CreateElement(element);
                if (attribute.Equals(""))
                    xe.InnerText = value;
                else
                    xe.SetAttribute(attribute, value);
                xn.AppendChild(xe);
            }
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时修改该节点属性值，否则修改节点值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * Update("/Node", "", "Value")
         * Update("/Node", "Attribute", "Value")
         ************************************************/
        public void Update(string node, string attribute, string value)
        {
            XmlNode xn = doc.SelectSingleNode(node);
            XmlElement xe = (XmlElement)xn;
            if (attribute.Equals(""))
                xe.InnerText = value;
            else
                xe.SetAttribute(attribute, value);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时删除该节点属性值，否则删除节点值</param>
        /**************************************************
         * 使用示列:
         * Delete("/Node", "")
         * Delete("/Node", "Attribute")
         ************************************************/
        public void Delete(string node, string attribute)
        {
            XmlNode xn = doc.SelectSingleNode(node);
            XmlElement xe = (XmlElement)xn;
            if (attribute.Equals(""))
                xn.ParentNode.RemoveChild(xn);
            else
                xe.RemoveAttribute(attribute);

        }
    }
}

/*
==================================================
XmlFile.xml：
<?xml version="1.0" encoding="utf-8"?>
<Root />
==================================================
使用方法：
string xml = Server.MapPath("XmlFile.xml");
//插入元素
//XmlHelper.Insert(xml, "/Root", "Studio", "", "");
//插入元素/属性
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
//插入属性
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
//修改元素值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
//修改属性值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
//读取元素值
//XmlHelper.Read("/Root/Studio/Site/Master", "") ;
//读取属性值
//Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
//读取特定属性值
//Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</div>");
//删除属性
//XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
//删除元素
//XmlHelper.Delete(xml, "/Root/Studio", "");

*/